<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="联系电话" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产业集群" prop="companyIndustry">
        <el-select v-model="queryParams.companyIndustry" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.industry_orientation"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="经济带" prop="economic">
        <el-select v-model="queryParams.economic" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.economic_belt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审批状态" prop="state">
        <el-select v-model="queryParams.state" clearable placeholder="请选择">
          <el-option
            v-for="dict in status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-select v-model="queryParams.sort" clearable placeholder="请选择">
          <el-option
            v-for="dict in sortType"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['information:userNeed:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['information:userNeed:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['information:userNeed:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userNeedList" @selection-change="handleSelectionChange" height="600" fixed>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户ID" align="center" prop="userId"/>
      <el-table-column label="联系电话" align="center" prop="phonenumber" min-width="150"/>
      <el-table-column label="名称" align="center" prop="companyName"/>
      <el-table-column label="需求方公司规模" align="center" prop="companyScale" min-width="130"/>
      <el-table-column label="产业集群" align="center" prop="companyIndustry" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.industry_orientation" :value="scope.row.companyIndustry"/>
        </template>
      </el-table-column>
      <el-table-column label="需求方公司介绍" align="center" prop="companyIntroduce" width="120">
        <template slot-scope="scope">
          <span>{{ changeCompanyIntroduce(scope.row.companyIntroduce) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="需求方证件类型" align="center" prop="identityType" width="120">
        <template slot-scope="scope">
          <div v-for="item in identityTypes" v-if="item.value === scope.row.identityType">
            <span>{{ item.label }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="需求方证件图片" align="center" prop="identityPhoto" width="120">
        <template slot-scope="scope">
          <image-preview :src="scope.row.identityPhoto" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="state">
        <template slot-scope="scope">
          <div style="" v-for="item in auditTypes" v-if="item.value === scope.row.state">
            <span>{{ item.label }}</span>
          </div>
        </template>
      </el-table-column>
<!--      <el-table-column label="审核状态" align="center" prop="state" width="95">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.audit_status" :value="scope.row.approvalState"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="关联产品体系" align="center" prop="approvalNote">
        <template slot-scope="scope">
          <span>{{ scope.row.approvalNote }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="身份" align="center" prop="userType">-->
      <!--        <template>-->
      <!--          <span>需求方</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="注册时间" align="center" prop="createTime" width="150"/>
      <el-table-column label="经济带" align="center" prop="economic" min-width="150"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" min-width="150"/>
      <el-table-column width="250" label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-row>
            <el-button
              v-if="scope.row.state==-1||scope.row.state==0"
              style=""
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleAudit(scope.row)"
              v-hasPermi="['information:userNeed:process']"
            >审核
            </el-button>
            <el-button
              style=""
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['information:userNeed:edit']"
            >
              <span v-if="$store.getters.userId===scope.row.userId">编辑</span>
              <span v-if="$store.getters.userId!==scope.row.userId">查看</span>
            </el-button>
            <el-button
              style=""
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['information:userNeed:remove']"
            >删除
            </el-button>
            <el-button
              style=""
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleResetPwd(scope.row)"
              v-hasPermi="['information:userNeed:edit']"
            >修改密码
            </el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改需求方对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal='false' width="600px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="名称" prop="nickName">
          <el-input class="kind" v-model="form.nickName" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phonenumber">
          <el-input class="kind" v-model="form.phonenumber" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <image-upload :limit="1" v-model="form.avatar"/>
        </el-form-item>
        <el-form-item label="需求方公司规模" prop="companyScale">
          <el-input class="kind" v-model="form.companyScale" placeholder="请输入需求方公司规模"/>
        </el-form-item>
        <el-form-item label="产业集群" prop="companyIndustry">
          <el-select class="kind" v-model="form.companyIndustry" placeholder="请选择产业集群">
            <el-option
              v-for="dict in dict.type.industry_orientation"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
          <el-input v-if="form.companyIndustry==='其他-'" class="kind" v-model="form.companyIndustryX"
                    placeholder="请输入产业集群"/>
        </el-form-item>
        <el-form-item label="关联产品体系" prop="approvalNote">
          <el-select class="kind" v-model="form.approvalNote" placeholder="请选择">
            <el-option
              v-for="dict in centerList"
              :key="dict.id+''"
              :label="dict.serviceName"
              :value="dict.serviceName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经济带" prop="economic">
          <el-select class="kind" v-model="form.economic" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.economic_belt"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="需求方公司介绍" prop="companyIntroduce">
          <el-input class="kind" v-model="form.companyIntroduce" type="textarea" placeholder="请输入需求方公司介绍"/>
        </el-form-item>
        <el-form-item label="需求方证件类型" prop="identityType">
          <el-select class="kind" v-model="form.identityType" placeholder="请选择">
            <el-option
              v-for="dict in identityTypes"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="需求方证件图片" prop="identityPhoto">
          <image-upload v-model="form.identityPhoto"/>
        </el-form-item>
        <el-form-item v-if="title==='添加需求方'" label="密码" prop="password">
          <el-input class="kind" v-model="form.password" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="审核备注" v-if="form.state!=-1&&form.state!=0" prop="epprovalNote">
          <el-input class="kind" disabled type="textarea" v-model="form.epprovalNote"/>
        </el-form-item>
        <el-form-item label="完善信息状态" prop="perfectionState">
          <el-select style="width: 60%" v-model="form.perfectionState" placeholder="请选择完善信息状态">
            <el-option
              v-for="dict in commitType"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
          <el-tooltip class="item" effect="dark" content="所有必填项填写完成，且进度不少于80%，注册成功"
                      placement="right">
            <el-button>提示</el-button>
          </el-tooltip>
        </el-form-item>
        <el-progress :percentage="compared" :format="format"></el-progress>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="$store.getters.userId===form.userId||title=='添加需求方'" @click="submitForm">确
          定
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改需求方对话框 -->
    <el-dialog title="审核" :visible.sync="open2" :close-on-click-modal='false' width="600px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="审核状态" prop="state">
          <el-select class="kind" v-model="form.state" placeholder="请选择审核状态">
            <el-option
              v-for="dict in auditTypes"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核备注" prop="epprovalNote">
          <el-input class="kind" type="textarea" v-model="form.epprovalNote" placeholder="请输入审核备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addUserNeed, delUserNeed, getUserNeed, listUserNeed, updateUserNeed} from "@/api/information/userNeed";
import {resetUserPwd} from "@/api/system/user";
import {listCenter} from "@/api/information/center";
import MyEditor from "@/components/wangEdiot/index.vue";

export default {
  name: "UserNeed",
  components: {MyEditor},
  dicts: ['industry_orientation', 'economic_belt','audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 需求方表格数据
      centerList: [],
      userNeedList: [],
      commitType: [
        {
          value: 0,
          label: "未完善"
        },
        {
          value: 1,
          label: "已完善"
        },
      ],
      identityTypes: [
        {
          value: 0,
          label: "法人身份证"
        },
        {
          value: 1,
          label: "营业执照"
        },
      ],
      auditTypes: [
        {
          value: -1,
          label: "未提交"
        },
        {
          value: 0,
          label: "待审核"
        }, {
          value: 1,
          label: "审核通过"
        },
        {
          value: 2,
          label: "审核失败"
        },
      ],
      sortType: [
        {
          value: 0,
          label: "按时间倒序"
        },
        {
          value: 1,
          label: "按时间正序"
        },
      ],
      status: [
        {
          value: -1,
          label: "未提交"
        },
        {
          value: 0,
          label: "待审批"
        },
        {
          value: 1,
          label: "审批通过"
        },
        {
          value: 2,
          label: "审批失败"
        },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        companyScale: null,
        companyIndustry: null,
        companyIntroduce: null,
        identityType: null,
        identityPhoto: null,
        perfectionState: null,
        approvalState: null,
        approvalNote: null,
        nickName: null,
        phonenumber: null,
        sort: null,
        state: null,
        park: null,
        economic: null,
        address: null,
        area: null,
        city: null,
        province: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [
          {required: true, message: "公司名称不能为空", trigger: "blur"}
        ],
        companyScale: [
          {required: true, message: "需求方公司规模不能为空", trigger: "blur"}
        ],
        companyIndustry: [
          {required: true, message: "产业集群不能为空", trigger: "blur"}
        ],
        companyIndustryX: [
          {required: true, message: "产业集群不能为空", trigger: "blur"}
        ],
        companyIntroduce: [
          {required: true, message: "需求方公司介绍不能为空", trigger: "blur"}
        ],
        identityType: [
          {required: true, message: "需求方证件类型不能为空", trigger: "change"}
        ],
        identityPhoto: [
          {required: true, message: "需求方证件图片不能为空", trigger: "blur"}
        ],
        approvalState: [
          {required: true, message: "审核状态不能为空", trigger: "blur"}
        ],
        phonenumber: [
          {required: true, message: "请输入联系电话", trigger: "blur"},
        ],
        password: [
          {required: true, message: "密码不能为空", trigger: "blur"}
        ],
        nickName: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        remark: [
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
      }
    };
  },
  computed: {
    compared() {
      let size = 0;
      if (this.form.nickName != null && this.form.nickName != "") {
        size = size + 10;
      }
      if (this.form.phonenumber != null && this.form.phonenumber != "") {
        size = size + 10;
      }
      if (this.form.password != null && this.form.password != "") {
        size = size + 10;
      }
      if (this.form.companyScale != null && this.form.companyScale != "") {
        size = size + 10;
      }
      if (this.form.identityPhoto != null && this.form.identityPhoto != "") {
        size = size + 10;
      }
      if (this.form.companyIntroduce != null && this.form.companyIntroduce != "") {
        size = size + 10;
      }
      if (this.form.companyIndustry != null && this.form.companyIndustry != "") {
        size = size + 10;
      }
      if (this.form.identityType != null) {
        size = size + 10;
      }

      if (size >= 80) {
        this.form.perfectionState = 1
      } else {
        this.form.perfectionState = 0
      }

      if (this.form.avatar != null && this.form.avatar != "") {
        size = size + 10;
      }

      if (this.form.approvalNote != null && this.form.approvalNote != "") {
        size = size + 10;
      }

      return size;
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 转化富文本去除html格式*/
    changeCompanyIntroduce(richtext) {
      if (richtext != "" && richtext != null) {
        let len = 10
        let content = richtext.replace(/<.+?>/g, '');
        content = content.replace(/ /ig, ''); /* 去除  */
        content = content.replace(/\s/ig, ''); /* 去除空格 */
        if (content.length > len) {
          content = content.substring(0, len) + '...';
        }
        return content;
      }
      return "";
    },
    format(percentage) {
      return percentage === 100 ? '已完成' : `${percentage}%`;
    },
    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.$prompt('请输入"' + row.userName + '"的新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^.{5,20}$/,
        inputErrorMessage: "用户密码长度必须介于 5 和 20 之间"
      }).then(({value}) => {
        resetUserPwd(row.userId, value).then(response => {
          this.$modal.msgSuccess("修改成功，新密码是：" + value);
        });
      }).catch(() => {
      });
    },
    /** 查询需求方列表 */
    getList() {
      this.loading = true;
      listUserNeed(this.queryParams).then(response => {
        this.userNeedList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

      listCenter({parentId: 0}).then(response => {
        this.centerList = response.data
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        companyName: null,
        companyScale: null,
        companyIndustry: null,
        companyIndustryX: null,
        companyIntroduce: null,
        identityType: null,
        identityPhoto: null,
        perfectionState: null,
        approvalState: null,
        approvalNote: null,
        nickName: null,
        phonenumber: null,
        sort: null,
        state: -1,
        park: null,
        economic: null,
        address: null,
        area: null,
        city: null,
        province: null,
        remark: null,
        password: null,
        epprovalNote: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加需求方";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getUserNeed(userId).then(response => {
        this.form = response.data;
        if (this.form.companyIndustry != null) {
          if (this.form.companyIndustry.includes("其他-")) {
            var a = this.form.companyIndustry.split("-");
            this.form.companyIndustryX = a[1];
            this.form.companyIndustry = "其他-"
          }
        }
        this.open = true;
        if (this.$store.getters.userId !== this.form.userId) {
          this.title = "需求方详情";
        } else {
          this.title = "修改需求方";
        }
      });
    },
    /** 修改按钮操作 */
    handleAudit(row) {
      this.reset();
      const userId = row.userId || this.ids
      getUserNeed(userId).then(response => {
        this.form = response.data;
        this.open2 = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.companyName = this.form.nickName
          if (this.form.companyIndustry === '其他-') {
            this.form.companyIndustry = this.form.companyIndustry + this.form.companyIndustryX
          }
          if (this.form.userId != null) {
            updateUserNeed(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.open2 = false;
              this.getList();
            });
          } else {
            this.form.approvalState = 1
            if (this.form.companyIndustry === '其他-') {
              this.form.companyIndustry = this.form.companyIndustry + this.form.companyIndustryX
            }
            addUserNeed(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除需求方编号为"' + userIds + '"的数据项？').then(function () {
        return delUserNeed(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('information/userNeed/export', {
        ...this.queryParams
      }, `userNeed_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.kind {
  width: 90%;
}
</style>
